#ifndef XXactivityIndicatorView_h
#define XXactivityIndicatorView_h

#include <QWidget>
#include <QPaintEvent>
#include <QTimerEvent>
#include <QPushButton>
#include <functional>

class XXactivityIndicatorView : public QWidget
{
    Q_OBJECT
public:
    XXactivityIndicatorView(QWidget *parent = nullptr);
    virtual ~XXactivityIndicatorView();

    // config
    void config(const QVariantMap &param);
    void configItem(int margin, int padding, QSize size, int count, int activeCount, QColor normal, QColor active);
    void configBackgroundColor(QColor bgColor, qreal radius, QWidget *centerAt);
    void configCancel(const QString &title, int height=35, std::function<bool(void)> willCancelBlock=nullptr);
    void configWindowModal();

    const static char * const kItemPadding;
    const static char * const kItemMargin;
    const static char * const kItemSize;
    const static char * const kItemCount;
    const static char * const kActiveItemCount;

    const static char * const kNormalColor;
    const static char * const kActiveColor;
    const static char * const kBgColor;
    const static char * const kBgRadius;
    const static char * const kCenterAt;

    // action
    void setWillCancelBlock(std::function<bool(void)> block);
    Q_SLOT void show();
    Q_SLOT void hide();

protected:
    //QSize sizeHint() const;
    bool eventFilter(QObject *watcher, QEvent *event);
    void paintEvent(QPaintEvent *event);
    void timerEvent(QTimerEvent *event);

private:
    void start();
    void stop();
    void updatePosition();

private:
    qreal _itemPadding;
    qreal _itemMargin;
    QSizeF _itemSize;
    qreal _itemCount;
    qreal _activeItemCount;
    QColor _normalColor;
    QColor _activeColor;
    QColor _bgColor;
    qreal _bgRadius;
    QWidget *_centerAt;

    int _timerID;
    int _activeIndex;
    /** 是否铺满parentWidget，默认false */
    bool _isFullAtParent;

    std::function<bool(void)> _willCancelBlock;
    QPushButton *_cancelButton;
};

#endif
